﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using com.paypal.sdk.services;
using com.paypal.sdk.profiles;
using com.paypal.sdk.util;
namespace DigitalGoodsExample
{
    public partial class _return : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String payerid = Request.QueryString["PAYERID"];
            String token = Request.QueryString["TOKEN"];
            NVPCallerServices caller2 = new NVPCallerServices();
            IAPIProfile profile2 = ProfileFactory.createSignatureAPIProfile();
            /*WARNING: Do not embed plaintext credentials in your application code.
            Doing so is insecure and against best practices.Your API credentials must 
            be handled securely. Please considerencrypting them for use in any production 
            environment, and ensurethat only authorized individuals may view or modify them.*/

            // Set up your API credentials, PayPal end point, API operation and version.

            profile2.APIUsername = Constants.API_USERNAME;
            profile2.APIPassword = Constants.API_PASSWORD;
            profile2.APISignature = Constants.API_SIGNATURE;
            profile2.Environment = Constants.ENVIRONMENT;
            caller2.APIProfile = profile2;
            NVPCodec encoder2 = new NVPCodec();
            encoder2["VERSION"] = "65.1";
            encoder2["METHOD"] = "DoExpressCheckoutPayment";

            // Add request-specific fields to the request.
            encoder2["PAYERID"] = payerid;
            encoder2["TOKEN"] = token;
            encoder2["PAYMENTREQUEST_0_CURRENCYCODE"] = "USD";
            encoder2["PAYMENTREQUEST_0_PAYMENTACTION"] = "SALE";
            encoder2["PAYMENTREQUEST_0_AMT"] = "300.00";
            encoder2["PAYMENTREQUEST_0_ITEMAMT"] = "200.00";
            encoder2["PAYMENTREQUEST_0_TAXAMT"] = "100.00";
            encoder2["PAYMENTREQUEST_0_DESC"] = "Movies";
            encoder2["L_PAYMENTREQUEST_0_ITEMCATEGORY0"] = "Digital";
            encoder2["L_PAYMENTREQUEST_0_ITEMCATEGORY1"] = "Digital";
            encoder2["L_PAYMENTREQUEST_0_NAME0"] = "Movie 1";
            encoder2["L_PAYMENTREQUEST_0_NAME1"] = "Movie 2";
            encoder2["L_PAYMENTREQUEST_0_NUMBER0"] = "10101";
            encoder2["L_PAYMENTREQUEST_0_NUMBER1"] = "20202";
            encoder2["L_PAYMENTREQUEST_0_QTY0"] = "1";
            encoder2["L_PAYMENTREQUEST_0_QTY1"] = "1";
            encoder2["L_PAYMENTREQUEST_0_TAXAMT0"] = "50.00";
            encoder2["L_PAYMENTREQUEST_0_TAXAMT1"] = "50.00";
            encoder2["L_PAYMENTREQUEST_0_AMT0"] = "100.00";
            encoder2["L_PAYMENTREQUEST_0_AMT1"] = "100.00";
            encoder2["L_PAYMENTREQUEST_0_DESC0"] = "Download";
            encoder2["L_PAYMENTREQUEST_0_DESC1"] = "Download";

            // Execute the API operation and obtain the response.
            string DoECStr = encoder2.Encode();
            string DoECResp = caller2.Call(DoECStr);
            NVPCodec decoder2 = new NVPCodec();
            decoder2.Decode(DoECResp);
            string[] resp = DoECResp.Split('&');
            int iresp = resp.Length - 1;

            //there has to be a better way to do this with Hash Tables.

            foreach (string sresp in resp)
            {
                string[] aresp = sresp.Split('=');
                if (aresp.Length > 1)
                {
                    string sKey = aresp[0];
                    string sVal = aresp[1];
                    if (sKey == "ACK")
                    {
                        if (sVal == "Success" || sVal == "SuccessWithWarning")
                        {
                            Response.Write("Transaction Succeeded! Download your product <a href='#'>HERE</a>.");
                        }
                        //end if
                        else
                        {
                            Response.Write("Transaction Failed" + DoECResp);
                        }
                        //end else
                    }
                    //end if
                }
                //end if
            }
            //end foreach
        }
    }
}